package com.jc.controller;
import com.jc.entity.User;
import com.jc.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.http.client.support.HttpRequestWrapper;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.HttpRequestHandler;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;

/**
 * @author luoyong
 * @Date 2021.3.19
 * @Description 用户控制类
 */
@RestController
@RequestMapping(value="/index")
public class UserController {
        @Autowired
        private UserService userService;

        @RequestMapping(value="login")
        @ResponseBody
        public HashMap<String, String> getUser(@RequestParam(value="name",required=true) String name, @RequestParam(value="password",required=true) String password, HttpServletRequest httprequest){
                User users=userService.getUser(name,password);
                //将登陆用户存储进会话session中
                HttpSession httpsession=httprequest.getSession();
                HashMap<String,String> data=new HashMap<String, String>();
                if(users!=null){
                        //设置用户会话session
                        httpsession.setAttribute("userSession",users);
                        data.put("code","200");
                        data.put("message","校验成功");
                        return data;
                }else{
                        data.put("code","400");
                        data.put("message","校验失败");
                        return null;
                }
        }

        public String addUser(User user){
                userService.insertUser(user.getName(),user.getPassword());
                return "";
        }
        public String deleteUser(Integer id){
                userService.deleteUser(id);
                return "";
        }

        //跳转到登陆页面
        @RequestMapping(value="toLogin")
        public ModelAndView toLogin(Model model){
                return new ModelAndView("index");
        }

        //跳转到首页
        @RequestMapping(value="index3")
        public ModelAndView firstPage(Model model,HttpServletRequest request){
                //判断当前用户是否登陆状态，未登录则跳转到登陆页面
                User user= (User) request.getSession().getAttribute("userSession");
                if(user==null){
                        return new ModelAndView("index");
                }
                return new ModelAndView("index3");
        }

        @RequestMapping(value="home")
        public ModelAndView home(Model model){
                return new ModelAndView("home");
        }

        //加载高德地图
//        @RequestMapping(value="map")
//        public ModelAndView map(){
//                System.out.println("请求成功过来------------------------");
//                return new ModelAndView("map");
//        }

//        //**获取高德地图
//        @RequestMapping(value="gaode")
//        public ModelAndView gaode(){
//                return new ModelAndView("gaode");
//        }
}